﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;


/// <summary>
/// Summary description for BLLShipment
/// </summary>
public class BLLShipment
{

    public BLLShipment()
    {

    }

    public void NewCustomer(tblCustomer customer)
    {
        DALCustomer dalCustomer = new DALCustomer();
        dalCustomer.insertCustomer(customer);
    }

    public List<tblParcelType> getAllParcelType()
    {
        DALParcelType dal = new DALParcelType();
        return dal.getAllParcelType();
    }

    public void NewPackage(tblPackage package)
    {

        DALPackage dalPackage = new DALPackage();

        //Insert New Package
        dalPackage.insertPackage(package);

        //Update BarCode
        package.Barcode = DateTime.Today.ToString("ddMMyyyy") + package.ID.ToString("000000") + RandomString(3);
        dalPackage.updatePackage(package);

        // Create payment
        addPayment(package);
    }

    private string RandomString(int Size)
    {
        Random random = new Random((int)DateTime.Now.Ticks);
        string input = "abcdefghijklmnopqrstuvwxyz0123456789";
        var chars = Enumerable.Range(0, Size)
                               .Select(x => input[random.Next(0, input.Length)]);
        return new string(chars.ToArray());
    }

    private void addPayment(tblPackage package)
    {
        DALPayment dalPayment = new DALPayment();
        tblPayment payment = new tblPayment();

        decimal countryFee = package.tblCustomerReceiver.tblCountry.PriceRate.Value;
        decimal additonalFee = package.tblParcelType.AdditionalFee.Value;
        payment.Price = package.Weight.Value * countryFee + additonalFee;

        payment.PackageID = package.ID;
        payment.IsDone = false;
        payment.CreatedDate = DateTime.Now;
        dalPayment.insertPayment(payment);
    }

    /// <summary>
    /// Generate Receipt 
    /// After people register a new shipment
    /// </summary>
    /// <param name="packageid">Package ID</param>
    /// <param name="filename">the path to save PDF File</param>
    /// <param name="oFont">Bar code Font free3of9.ttf</param>
    public void GenerateReceipt(int packageid, string filename, System.Drawing.Font oFont)
    {
        DALPackageDetail dalPackageDetail = new DALPackageDetail();
        vPackageDetail package = dalPackageDetail.getPackageDetail(packageid);

        Document doc = new Document();
        PdfWriter.GetInstance(doc, new FileStream(filename, FileMode.Create));
       // string html = "";
        doc.Open();
        doc.AddTitle("Receipt");
        doc.Add(new Paragraph("Recepit"));
        doc.Add(new Paragraph("Item Type: " + package.ParcelType));
        doc.Add(new Paragraph("Weight: " + package.Weight.Value.ToString("###.00") + "Kg"));
        doc.Add(new Paragraph("Cost: $" + package.Price.Value.ToString("###.00")));
        doc.Add(new Paragraph("Sender: " + package.Sender));
        doc.Add(new Paragraph("Receiver: " + package.Receiver));
        doc.Add(new Paragraph("Receiver Address: " + package.ReceiverLocation));
        doc.Add(new Paragraph("Reference Code : "));

        //iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
        //iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(doc);
        //hw.Parse(new StringReader(HTML));

        doc.Add(generateBarcodeImage(oFont, package.Barcode));
        doc.Add(generateBarcodeImage(oFont, package.Barcode, 300, 100));

        doc.Close();
    }

    private iTextSharp.text.Image generateBarcodeImage(System.Drawing.Font oFont, string barcode)
    {
        Stream stream = new MemoryStream();
        generateBarcode(barcode, oFont).Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
        stream.Position = 0;
        iTextSharp.text.Image imgBarcode = iTextSharp.text.Image.GetInstance(stream);
        imgBarcode.ScaleToFit(400, 100);
        return imgBarcode;
    }

    private iTextSharp.text.Image generateBarcodeImage(System.Drawing.Font oFont, string barcode, int w, int h)
    {
        iTextSharp.text.Image imgBarcode = generateBarcodeImage(oFont, barcode);
        imgBarcode.ScaleToFit(w, h);
        return imgBarcode;
    }




    public List<tblCountry> getCountries()
    {
        DALCountry dal = new DALCountry();
        return dal.getCountries();
    }

    private Bitmap generateBarcode(string Code, System.Drawing.Font oFont)
    {
        int w = Code.Length * 50;
        int h = 100;

        Bitmap oBitmap = new Bitmap(w, h);

        // then create a Graphic object for the bitmap we just created.
        Graphics oGraphics = Graphics.FromImage(oBitmap);

        // Let's create the Point and Brushes for the barcode
        PointF oPoint = new PointF(2f, 2f);
        SolidBrush oBrushWrite = new SolidBrush(Color.Black);
        SolidBrush oBrush = new SolidBrush(Color.White);

        // Now lets create the actual barcode image
        // with a rectangle filled with white color
        oGraphics.FillRectangle(oBrush, 0, 0, w, h);

        // We have to put prefix and sufix of an asterisk (*),
        // in order to be a valid barcode
        oGraphics.DrawString("*" + Code + "*", oFont, oBrushWrite, oPoint);

        return oBitmap;
    }

}
